package com.crowd.web.controller;

import com.crowd.entity.Auth;
import com.crowd.entity.Role;
import com.crowd.service.api.IAdminService;
import com.crowd.service.api.IAuthService;
import com.crowd.service.api.IRoleService;
import com.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * @author Stone
 * @date 2020/5/24 15:12
 */
@Controller
public class AssignController {

    Logger logger = LoggerFactory.getLogger(AssignController.class);

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IAuthService authService;

    /**
     * @Author:Stone
       * @Param adminId:
     * @Param model:
       * @return: java.lang.String
     * @Date: 2020/5/26 10:43
     * @Description:跳转到角色分配页面，并且列出已分配角色和未分配角色
    */
    @RequestMapping("/assign/to/assign/role/page")
    public String toAssignPage(@RequestParam(value = "adminId") Integer adminId,
                               Model model){
        logger.info("adminId:"+adminId.toString());
        // 1.找到已分配给此用户的角色list
        List<Role> assignedRoleList  = roleService.getAssignedRoleList(adminId);
        // 2.找到未分配给此用户的角色list
        List<Role> unAssignedRoleList = roleService.getUnAssignedRoleList(adminId);
        // 3.将两个列表返回到页面
        model.addAttribute("assignedRoleList", assignedRoleList);
        model.addAttribute("unAssignedRoleList",unAssignedRoleList);
        return "/assign-role";
    }

    /**
     * @Author:Stone
       * @Param pageNum:
     * @Param keyword:
     * @Param adminId:
     * @Param assignedRoleList:
       * @return: java.lang.String
     * @Date: 2020/5/26 15:11
     * @Description:执行分配新角色的功能
    */
    @RequestMapping("/assign/do/role/assign")
    public String saveAdminRoleRelationship(@RequestParam(value = "pageNum") Integer pageNum,
                                            @RequestParam(value = "keyword") String keyword,
                                            @RequestParam(value = "adminId") Integer adminId,
                                            // required = false表示：这个assignedRoleList不是必要数据，空也可以传过来
                                            // 因为需求，可能是拉黑操作，把这个用户的所有角色都清空了
                                            @RequestParam(value = "assignedRoleList",required = false) List<Integer> assignedRoleList){
        roleService.saveAdminRoleRelationship(adminId,assignedRoleList);
        return "redirect:/admin/get/page.html"+"?pageNum="+pageNum+"&keyword="+keyword;
    }

    /**
     * @Author:Stone

       * @return: com.crowd.util.ResultEntity<java.util.List<com.crowd.entity.Auth>>
     * @Date: 2020/5/26 18:52
     * @Description:获取所有权限
    */
    @RequestMapping("/assgin/get/all/auth")
    @ResponseBody
    public ResultEntity<List<Auth>> getAuthList(){
        try{
            List<Auth> authList = authService.getAuthList();
            return ResultEntity.successWithData(authList);
        }catch (Exception e){
            return ResultEntity.failed(e.getMessage());
        }
    }

    /**
     * @Author:Stone
       * @Param roleId:
       * @return: com.crowd.util.ResultEntity<java.util.List<java.lang.Integer>>
     * @Date: 2020/5/26 18:56
     * @Description:通过roleId去中间表去已经分配的权限
    */
    @RequestMapping("/assign/get/assigned/auth/id/by/role/id")
    @ResponseBody
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(@RequestParam(value = "roleId") Integer roleId){
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successWithData(authIdList);
    }

    /**
     * @Author:Stone
       * @Param map: key为authIdArray，roleId
       * @return: com.crowd.util.ResultEntity<java.lang.String>
     * @Date: 2020/5/27 16:45
     * @Description:保存新分配的权限
    */
    @RequestMapping("/assign/do/role/assign/auth")
    @ResponseBody
    public ResultEntity<String> saveRoleAuthRelathinship(@RequestBody Map<String,List<Integer>> map){
        try{
            authService.saveRoleAuthRelathinship(map);
            return ResultEntity.successWithoutData();
        }catch (Exception e){
            return ResultEntity.failed(e.getMessage());
        }

    }

}
